// pages/Goods/Address/index.js
var common = require("../../../../public.js");
// var QQMapWX = require('../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
const QQMapWX = require('../../../../../libs/qqmap-wx-jssdk1.2/qqmap-wx-jssdk') //腾讯
var qqmapsdk;

Page({
    /**
     * 页面的初始数据
     */
    data: {
        rangeShow:false,
        token: '',
        userInfo: {},
        addressList: [],
        from: '', //页面进入
        active: '',
        isLoadingBusy: false,
        paramArr: [],
        addressLists:[]
    },
    /**
     * 生命周期函数--监听页面加载
     */


    onLoad: function (opt) {
        console.log(opt,"来自哪里")
        console.log('aaa----',opt.from)
        // JSON.parse(options.params)
        const that = this
        
        common.getLocalUserInfo(function (success, data) {
            if (success) {
                that.setData({
                    userInfo: data.user,
                    token: data.token,
                    itemId: opt.itemId ? opt.itemId : '',
                    storeId: opt.storeId ? opt.storeId : 1641,
                    from: opt.from ? opt.from : '',
                    storePrice: opt.storePrice ? opt.storePrice : '',
                    active: opt.active ? opt.active : '',
                    params: opt.orderInfo ? JSON.parse(opt.orderInfo) : '',

                }, function () {
                    that.getList()
                    // that.getIsList()
                })
            }
        })

        // that.mapCtx = wx.createMapContext('myMap')
        // 实例化API核心类
        qqmapsdk = new QQMapWX({
            //   key: 'W57BZ-JDB6X-XPA4H-Z76MI-73FF2-24BT4'
            key: 'SUOBZ-SJG6B-PQQUT-JL75Z-3F3Z5-AAFJ3'// 原P35BZ-K2WAS-GUZO7-6IRVW-IYBQV-ZUB6B
        });

    },
    onShow() {
        // this.getList()
    },
    addAddress() { // 添加新地址
        if(this.data.addressLists.length>=5){
          common.showToast('最多可添加5条地址')
          return
        }
        wx.navigateTo({
            url: '/packageA/pages/Goods/Address/addAddress/index?from=0&storeId=' + this.data.storeId,
        })
    },
    changeAddress(e) { // 修改地址
        // console.log('address',e.currentTarget.dataset.item)
        console.log(e,"4444444")
      
        let item = JSON.stringify(e.currentTarget.dataset.item)
        console.log('oooo====',item);
        wx.navigateTo({
            url: '/packageA/pages/Goods/Address/addAddress/index?from=1&item=' + item +"&storeId=" + this.data.storeId,
        })
    },


    // 地址列表
    getList() {
        this.setData({
            isLoadingBusy: true
        })
        const that = this
        const param = {
            token: that.data.token,
            userId: that.data.userInfo.id,
            storeId:that.data.storeId,
        }
        if (that.data.addressLists.length > 0) {
            that.setData({
                addressLists: []
            })
        }
        if (that.data.paramArr.length > 0) {
            that.setData({
                paramArr: []
            })
        }

        common.getJson('/user/ship/listForStoreTakeaway', param, function (res) {
            if (res.status === 200) {
                that.setData({
                    addressLists: res.data
                }, () => {
                    // res.data.forEach((item, index) => {
                    //     that.addressAnalysis(item.receiverAddress, index, item)
                      
                    // })
                

                })

            }

        },
            function () {
                setTimeout(function () {
                    that.setData({
                        isLoadingBusy: false
                    })
                }, 200);
            })
    },
    // addressAnalysis(address, index, item) {
    //     // return new Promise((resolve, rej) => {
    //     const that = this
    //     qqmapsdk.geocoder({
    //         //获取表单传入地址
    //         address: address, //地址
    //         success: function (res) {//成功后的回调
    //             that.data.paramArr.push({
    //                 storeId: that.data.storeId,
    //                 shipId: item.id,
    //                 xpoint: res.result.location.lng,
    //                 ypoint: res.result.location.lat
    //             })
    //             if(that.data.paramArr.length==that.data.addressLists.length){
    //                 console.log(that.data.addressLists, "kkkkkkkkkkkkkk")
    //                 that.getIsList(that.data.paramArr)  //是否可以配送  
    //             }else{
    //                 // console.log(that.data.addressLists, "kkkkkkkkkkkkkk")
    //             }
    //         },
    //         fail: function (error) {
    //             common.showToast(error)
    //         },
    //         complete: function (res) {
    //             // console.log(index,"内")

    //             // resolve(res.result.location)
    //         }
    //     })
    //     // })
    // },
  
    //   验证是否在商家可配送范围
    // getIsList(param) {
    //     const that = this
    //     this.setData({
    //         isLoadingBusy: true
    //     })
    //     if (that.data.addressList.length > 0) {
    //         that.setData({
    //             addressList: []
    //         })
    //     }

    //     common.postJson('/store/getStoreIsDeliveredList', param, function (res) {
    //         if (res.status === 200) {
    //               console.log(res,"pjjjjjjjjjjjjjjjjjj")
    //             that.setData({
    //                 addressList: res.data
    //             },()=>{
    //                 console.log(that.data.addressList,"ikipppp")
    //             })
    //         }
    //     },
    //         function () {
    //             setTimeout(function () {
    //                 that.setData({
    //                     isLoadingBusy: false
    //                 })
    //             }, 200);
    //         })
    // },


    clickgetId(e) {
        let that = this
        if (e.currentTarget.dataset.deliveredflag == 2 && (this.data.from=='tobepaidCommit' || this.data.from=='takeoutOrder')) {
            // common.showToast('超出范围')
            this.setData({
                rangeShow:true,
            })
            return
        }
        if (this.data.from == "mySelf") return;
        var pages = getCurrentPages()
        // console.log('page-22-_',pages);
        var prevPage = pages[pages.length - 2] //上一个页面

        // if (this.data.from == 'crowdOrder') {
        //     console.log(that.data.params, "55555")
        //     const paramss = {
        //         selectedAddr: true, // false代表已经选择地址
        //         receiverName: e.currentTarget.dataset.name,
        //         receiverMobile: e.currentTarget.dataset.phone,
        //         longitude: e.currentTarget.dataset.xpoint,
        //         latitude: e.currentTarget.dataset.ypoint,
        //         receiverAddress: e.currentTarget.dataset.address,
        //         id: e.currentTarget.dataset.id,
        //         crowdfundingId: this.data.params.crowdfundingId,
        //         setmeal: this.data.params.setmeal,
        //         payNum: this.data.params.payNum,
        //         storeId: this.data.params.storeId,
        //         storeName: '',
        //         storeImage: '',

        //     }
        //     console.log(paramss, "000000")
        //     var params = JSON.stringify(paramss)
        //     wx.navigateBack({
        //         delta: 1,
        //         success: function () {
        //             prevPage.onLoad(params)
        //         }
        //     })
        //     return
        // }
        // if(that.data.from == 'tobepaidCommit'){
           
        //     wx.navigateBack({
        //         delta: 1,
        //         success: function () {
        //             prevPage.setData({
        //                 receiverAddress:e.currentTarget.dataset.address
        //             })
        //             prevPage.data.receiverAddress = e.currentTarget.dataset.address
        //         }
        //     })
        //     return
        // }
        // console.log('这里啦----');
        var options = {
            // active: 2,
            storeId: that.data.storeId,
            itemId: that.data.itemId,
            selectedAddr: true, // false代表已经选择地址
            receiverName: e.currentTarget.dataset.name,
            receiverMobile: e.currentTarget.dataset.phone,
            longitude: e.currentTarget.dataset.xpoint,
            latitude: e.currentTarget.dataset.ypoint,
            receiverAddress: e.currentTarget.dataset.address ,
            id: e.currentTarget.dataset.id,
            storePrice: that.data.storePrice,
            StoPrice:that.data.StoPrice,
        }
        // this.changeFunc(options);
        this.getPostType(options);
        // wx.navigateBack({
        //     delta: 1,
        //     success: function () {
        //         prevPage.onLoad(options)
        //     }
        // })




        // wx.navigateTo({
        //   url: "../Commit/index?active=2&storeId=" + this.data.storeId + '&itemId=' + this.data.itemId + '&receiverName=' + e.currentTarget.dataset.name + '&receiverMobile=' + e.currentTarget.dataset.phone + '&receiverAddress=' + e.currentTarget.dataset.address + '&index1=' + this.data.index1 + '&id=' + e.currentTarget.dataset.id
        // })
    },

    onClose() {
        this.setData({
            rangeShow: false
        })
    },
    onConfirm() {
        console.log('ssss');
        this.setData({
            rangeShow: false
        })
    },

    changeFunc(options){
console.log('options-++-',options);
    //   var options = {
    //     // active: 2,
    //     storeId: 1479,
    //     itemId: '',
    //     selectedAddr: true, // false代表已经选择地址
    //     receiverName: '11',
    //     receiverMobile: 17777777777777,
    //     longitude: 117.309967,
    //     latitude: 34.780231,
    //     receiverAddress:'地址222',
    //     id: 6840,
    //     storePrice:23
    // }
      var pages = getCurrentPages()
        console.log('f-fdsfdf');
        var prevPage = pages[pages.length - 2] //上一个页面
          wx.navigateBack({
            delta: 1,
            success: function () {
                prevPage.onLoad(options)
            }
        })
    },
    getPostType(options){
        var that = this
        var tempJson = {
            storeId: that.data.storeId,
            userId: that.data.userInfo.id,
            xpoint: options.longitude,
            ypoint: options.latitude
        }
console.log('3333',tempJson);
        common.post('/store/getStorePostTypeForBuyUser', tempJson, function (res) {
            if (res.status === 200) {
                // that.setData({
                //     postPrice: res.data[0].price ? res.data[0].price : 0,
                // }, function () {
                options.postPrice = res.data[0].price;
                    console.log('4444');
                    that.changeFunc(options);

                // })
            } else {
                // common.showRequestErrMsg(res)
            }
        }, function () {
            that.setData({
                loadingCount: that.data.loadingCount - 1
            })
        })
    },
    callBackFunc(options){
        console.log('aabbcc');
        var pages = getCurrentPages()
        console.log('page--_',pages);
        var prevPage = pages[pages.length - 2] //上一个页面
        options.postPrice = this.data.postPrice;
        wx.navigateBack({
            delta: 1,
            success: function () {
                prevPage.onLoad(prevPage,options)
            }
        })
        console.log('回调参数',options);
    }



})